// pages/main/available house/available.js
const url = "https://zbnczy.com"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    images:[],
    newArray:[],
    houseArray:[],
    defaultArray: [],
    twonIndex:0,
    twons:["张北县"],
    villageIndex:0,
    villages:["所有"],
    areaIndex:0,
    areaMin:0,
    areaMax:100000,
    areas:["所有","0~300","300~600","600~900","900以上"],
    propertyIndex:0,
    propertys:["集体"],
    tranYearIndex:0,
    tranMin:0,
    tranMax:0,
    tranYears:["所有","0~20","20~40","40~60","60及以上"],
    sortIndex:0,
    defaultSort:["恢复默认排序","按面积升序","按面积降序","按流转年限升序","按流转年限降序","按发布时间排序"],
    sortWay:["cover_area","transtime"],
    searchContext: "",
    maxPages: 1, 
    pagesNum: 1,
    tyindex: 1,
    frontPage: false,
    lastPage: true
  },
  //数据请求函数
  request(url, method = "GET") {
    return new Promise(
      (resolve, reject) => {
        wx.request({
          url,
          success:(res)=>{
            resolve(res);
          },
          fail:(e)=>{
            console.log(e);
          }
        })
      }
    )
  },
  //获得搜索文本函数
  getContext(e) {
    this.setData({
      searchContext: e.detail.value
    })
    console.log(this.data.searchContext);
  },
  searchHouse() {
    console.log(this.data.searchContext);
    this.handleConfirmInput()
  },
  //提交搜索要求函数
 handleConfirmInput() {
    this.request(`${url}/house/visit`)
    .then((res)=>{
      let res1 = res.data.filter((item => item.property=='集体'))
      this.setData({defaultArray:res1})
      const {defaultArray} = this.data
      //this.setData({searchContext: e.detail.value})
      console.log(this.data.searchContext);
     let newArray = defaultArray.filter(item => item.title.includes(this.data.searchContext))
     console.log(defaultArray)
     console.log(newArray)
      this.setData({defaultArray:newArray,houseArray:newArray.slice(0,5),maxPages:parseInt(newArray.length / 5)+Boolean(newArray.length % 5)})


      this.request(`${url}house/visitTown`)
      .then((res)=>{
        this.setData({twons:["所有",...res.data]})
      })
    })
    
  },
  //展示详细信息跳转函数
  JumpTo(houseinfo) {
    wx.navigateTo({
      url: `/pages/main/avaliablepro/info/info`,
      success: function (res) {
        let houseInfo = houseinfo.currentTarget.dataset.houseinfo
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('tranHouseInfo', {
          houseInfo
        })
      }
    })
  },
  setHouseArray(res){
    if(res ){
      this.setData({defaultArray:res})
      const {defaultArray} = this.data
      this.setData({houseArray:defaultArray.slice(0,5),pagesNum:1,maxPages:parseInt(defaultArray.length / 5)+Boolean(defaultArray.length % 5) })
     }else{
       this.setData({defaultArray:[],houseArray:[],maxPages:1})
     }
  },
  //分类展示结果函数
  bindPickerChange: function(e) {
    switch (e.currentTarget.dataset.num) {
      case 3:{
        this.setData({propertyIndex:parseInt(e.detail.value)})
        const {propertyIndex} = this.data
        if(propertyIndex == 0)
          this.setData({houseArray:this.data.defaultArray})
        if(propertyIndex == 1)
          this.setData({houseArray:this.data.defaultArray.slice(0,2)})
        if(propertyIndex == 2)
          this.setData({houseArray:this.data.defaultArray.slice(2,6)})
        if(propertyIndex == 3)
          this.setData({houseArray:[]})
      }
    }

    switch (e.currentTarget.dataset.num) {
      case 0:{
        this.setData({twonIndex:parseInt(e.detail.value)})
        const {twonIndex,twons} = this.data
        if(twonIndex == 0){
          this.setData({villages:["所有"]})
          this.onLoad()
        }else{
          this.request(`${url}/house/select/${twons[twonIndex]}/1`)
          .then((res)=>{
            console.log(res);
            let res1 = res.data.filter((item => item.property=="集体"))
            this.setHouseArray(res1)
            this.request(`${url}/house/select/${twons[twonIndex]}`)
            .then((res)=>{
              this.setData({villages:["所有",...res.data]})
            })
          })
        }
        break;
      }
      case 1:{
        this.setData({villageIndex:parseInt(e.detail.value)})
        const {twonIndex,twons,villageIndex,villages} = this.data
        if(villages[villageIndex] == "所有"){
          this.setData({areaMin:0,areaMax:100000})
          if(this.data.twonIndex == 0)
            this.onLoad()
            else{
              this.request(`${url}/house/select/${twons[twonIndex]}/1`)
              .then((res)=>{
                console.log(res);
                let res1 = res.data.filter((item => item.property=='集体'))
                console.log(res1);
                this.setHouseArray(res1)
              })
            }
        }
        this.request(`${url}/house/select/${twons[twonIndex]}/${villages[villageIndex]}`)
        .then((res)=>{
          console.log(res);
          let res1 = res.data.filter((item => item.property=="集体"))
          console.log(res1);
          this.setHouseArray(res1)
        })
        break;
      }
      case 2:{
        this.setData({areaIndex:parseInt(e.detail.value)})
        const {areaIndex,areas} = this.data
        console.log(areaIndex);
        if(areaIndex == 0){
          this.setData({twonIndex:0,villageIndex:0})
          const {twonIndex,twons,villageIndex,villages} = this.data
          if(twonIndex == 0){
            this.onLoad()
          }else{
            if(villageIndex == 0){
              this.request(`${url}/house/select/${twons[twonIndex]}/1`)
              .then((res)=>{
              console.log(res);
              this.setHouseArray(res)})
            }else{
              this.request(`${url}/house/select/${twons[twonIndex]}/${villages[villageIndex]}`)
              .then((res)=>{
              console.log(res);
              this.setHouseArray(res); })
            }
          }
          break;
        }else{
          this.setData({areaMin: (areaIndex-1)*300}) 
          if(areaIndex!=4)
            this.setData({areaMax: this.data.areaMin + 300}) 
          const {areaMin,areaMax} = this.data
          this.request(`${url}/house/select/cover_area/${areaMin}/${areaMax}`)
          .then((res)=>{
            console.log(res);
            let res1 = res.data.filter((item => item.property=="集体"))
            console.log(res1);
            this.setHouseArray(res1)
          })
          break;
        }
      }
      case 3:{

        this.setData({propertyIndex:parseInt(e.detail.value)})
        const {propertyIndex,propertys} = this.data
        this.setData({twonIndex:0,villageIndex:0})
        if(propertyIndex == 0){
          this.setData({twonIndex:0,villageIndex:0})
          this.onLoad()
        }else{
          this.request(`${url}/house/selectbyproperty/${propertys[propertyIndex]}`)
          .then((res)=>{
            console.log(res);
           this.setHouseArray(res)
          })
          break;
        }
          
      }
      case 4:{
        this.setData({tranYearIndex:parseInt(e.detail.value)})
        const {tranYearIndex,tranYears} = this.data
        if(tranYearIndex == 0){
          this.onLoad()
        }else{
          this.setData({areaMin:0,areaMax:100000})
          this.setData({tranMin: (tranYearIndex-1)*20}) 
          this.setData({tranMax: this.data.tranMin + 20}) 
          if(tranYearIndex == 4)
            this.setData({tranMax:10000})
          const {tranMax,tranMin} = this.data
          this.request(`${url}/house/select/transtime/${tranMin}/${tranMax}`)
          .then((res)=>{
            console.log(res);
            let res1 = res.data.filter((item => item.property=="集体"))
            console.log(res1);
            this.setHouseArray(res1)
          })
        }
        break;
      }
      case 5:{
        this.setData({sortIndex:parseInt(e.detail.value)})
        const {sortIndex,defaultSort,sortWay} = this.data
        if(sortIndex == 0){
         this.onLoad()
        }else{
          let min = 0,max=100000
          let sortWayIndex,soM;
          console.log(this.data.areaMin);
          if(sortIndex == 1||sortIndex == 2)
          {  sortWayIndex = 0; min = this.data.areaMin,max= this.data.areaMax}
          else if(sortIndex == 3||sortIndex == 4)
          {  sortWayIndex = 1;min = this.data.tranMin;max = this.data.tranMax}
          if(sortIndex == 1||sortIndex == 3)
            soM = 1;
          else if(sortIndex == 2||sortIndex == 4)
            soM = 2;
        
          this.request(`${url}/house/select/${sortWay[sortWayIndex]}/${soM}/${min}/${max}`)
          .then((res)=>{
            console.log("a"+res);
            let res1 = res.data.filter((item => item.property=="集体"))
            console.log(res1);
            this.setHouseArray(res1)
        })
        }
      }
      default:
        break;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  //页面初始加载函数
   onLoad: function (options) {
    this.request(`${url}/house/selectbyproperty/集体`)
    .then((res)=>{
      this.setData({defaultArray:res.data.sort((a,b)=> a.defaultSort-b.defaultSort)})
      const {defaultArray} = this.data
      this.setData({houseArray:defaultArray.slice(0,5),maxPages:parseInt(defaultArray.length / 5)+Boolean(defaultArray.length % 5) })
      this.request(`${url}/house/visitTown`)
      .then((res)=>{
        this.setData({twons:["张北县"]})
      })
    })
    this.setData({houseArray:this.data.defaultArray})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //翻页函数
  pagesFn: function (e) {
    let type = e.currentTarget.dataset.type;
    let number = e.currentTarget.dataset.number;
    if (typeof type == "string") { //上下页
      if (type == "previous_page") { //上一页
        if (this.data.pagesNum - 1 < 1) {
          return false
        }
        if ((this.data.pagesNum - 1) % 5 == 0) {
          if ((this.data.pagesNum - 6) <= 1) {
            this.setData({
              lastPage: true,
              frontPage: false,
            })
          }
          this.setData({
            lastPage: true,
            tyindex: this.data.pagesNum - 5,
            pagesNum: this.data.pagesNum - 1,
          })
        } else {
          this.setData({
            pagesNum: this.data.pagesNum - 1,
          })
        }
      } else if (type == "next_page") { //下一页
        if (this.data.pagesNum + 1 > this.data.maxPages) {
          return false
        }
        if (this.data.pagesNum % 5 == 0) {
          console.log(this.data.pagesNum + 5)
          console.log(this.data.maxPages)
          if ((this.data.pagesNum + 5) >= 5 ) {
            this.setData({
              lastPage: true,
              frontPage: true,
            })
          }
          if( (this.data.pagesNum + 5) >this.data.maxPages){
            this.setData({
              lastPage: false
            })
          }
          this.setData({
            tyindex: this.data.pagesNum + 1,
            pagesNum: this.data.pagesNum + 1,
          })
        } else {
          this.setData({
            pagesNum: this.data.pagesNum + 1,
          })
        }
      } else if (type == "start") { //第一页
        this.setData({
          pagesNum: 1,
          tyindex: 1,
          lastPage: true,
          frontPage: false,
        })
      } else { //最后一页
        if ((this.data.maxPages % 5) == 0) {
          this.setData({
            pagesNum: this.data.maxPages,
            tyindex: this.data.maxPages - 4,
            lastPage: false,
            frontPage: true,
          })
        } else {
          this.setData({
            pagesNum: this.data.maxPages,
            tyindex: this.data.maxPages + 1 - (this.data.maxPages % 5),
            lastPage: false,
            frontPage: true,
          })
        }
      }
    } else {
      this.setData({
        pagesNum: number
      })
    }
    console.log(this.data.pagesNum)
    // this.DataFn()可在此调用数据函数
    const {defaultArray,pagesNum} = this.data
    let pre = (pagesNum-1) * 5
    let next = pre + 5
    this.setData({houseArray:defaultArray.slice(pre,next)})
    console.log(this.data.houseArray);
  },
})